#include "eventsender.h"
#include "eventreceiver.h"
#include <algorithm>

void EventSender::addReceiver(EventReceiver *receiver, EventType type)
{
	mReceivers.insert(std::multimap<EventType, EventReceiver*>::value_type(type, receiver));
}

void EventSender::removeReceiver(EventReceiver *receiver, EventType type)
{
	std::multimap<EventType, EventReceiver*>::iterator it;
	it = std::find(mReceivers.begin(), mReceivers.end(), std::multimap<EventType, EventReceiver*>::value_type(type, receiver));
	mReceivers.erase(it);
}

void EventSender::send(EventType type)
{
	std::multimap<EventType, EventReceiver*>::iterator it;
	it = mReceivers.find(type);
	Event ev(this, type);
	while(it != mReceivers.end() && it->first == type)
	{
		it->second->receive(ev);
	}
}